﻿using HtmlAgilityPack;
using ProcessLib.Ratp.Easy.Breaker;
using ProcessLib.Ratp.Page.Processors;
using ProcessLib.Ratp.Page.Region;
using StyleSharp.Styles;

namespace ProcessLib.Ratp.Easy.ParagraphCompose;

public class PageComposer
{
    public StyleNode StyleNode
    {
        get;
        set;
    }

    public string PageDoc
    {
        get;
        set;
    }

    public List<LineRegion>? LineRegions
    {
        get;
        set;
    }

    public List<SemanticParagraph>? Paragraphs
    {
        get;
        set;
    }

    public string Outter
    {
        get;
        set;
    }
    public PageComposer(StyleNode styleNode, string page)
    {
        StyleNode = styleNode;
        PageDoc = page;
        Outter = Skeleton();
    }

    // 获取骨架
    public string Skeleton()
    {
        var document = new HtmlDocument();
        document.LoadHtml(PageDoc);
        var select = document.DocumentNode.SelectSingleNode("//div[contains(@class,'pf')]/div");
        if (select == null) throw new Exception();
        select.InnerHtml = "";
        return document.DocumentNode.OuterHtml;
    }
    // 组合成一个页面的完整内容
    public string Compose()
    {
        LineRegions = LineRegionParting.GetLineRegions(PageDoc);
        Paragraphs = XAndFFBreaker.Breaker(LineRegions,StyleNode);
        var process = new SpanProcessor();
        var main = process.Process(Paragraphs, StyleNode);
        // main代表的是一个页面的内部段落
        var document = new HtmlDocument();
        document.LoadHtml(Outter);
        var select = document.DocumentNode.SelectSingleNode("//div[contains(@class,'pf')]/div");
        select.InnerHtml = main;
        return document.DocumentNode.OuterHtml;
    }
}